import { Component, OnInit, Input } from '@angular/core';
import { MdlSnackbarService, MdlDialogService } from '@angular-mdl/core';
import { ReportConfigService } from '../report-config.service'
import { DialogService } from 'primeng/api';
import { ReportConfigFieldAddComponent } from '../base/report-config-fieldadd.component';

@Component({
    selector: 'report-config-statis',
    templateUrl: './report-config-statis.component.html',
    styleUrls: ['./report-config-statis.component.scss']
})
export class ReportConfigStatisComponent implements OnInit {
    currentReportId: number = 0;
    @Input()
    set reportId(report: number) {
        if (report) {
            this.currentReportId = report;
            this.reportConfigService.loadReportStatisGroupFields(this.currentReportId).subscribe(data => this.groups = data);
            this.reportConfigService.loadReportStatisSummaryFields(this.currentReportId).subscribe(data =>{
                console.log(data);
                this.summaries = data;
            });
        }
    }
    get reportId(): number { return this.currentReportId; }

    /** 分组字段 */
    groupCols: any[] = [
        { header: '字段名称', field: 'fieldDisplayName', style: { 'text-align': 'center', 'overflow': 'hidden', 'width': '15%' } },
        { header: '小计', field: 'sumFlag', style: { 'text-align': 'center', 'overflow': 'hidden', 'padding': 0, 'width': '15%' } },
        { header: '表头', field: 'tableHeader', style: { 'text-align': 'center', 'overflow': 'hidden', 'padding': 0 } }
    ]
    /** 汇总字段 */
    summaryCols: any[] = [
        { header: '字段名称', field: 'fieldDisplayName', style: { 'text-align': 'center', 'overflow': 'hidden', 'width': '15%' } },
        { header: '汇总方式', field: 'subType', style: { 'text-align': 'center', 'overflow': 'hidden', 'padding': 0, 'width': '15%' } },
        { header: '反查明细', field: 'detailFlag', style: { 'text-align': 'center', 'overflow': 'hidden', 'padding': 0, 'width': '10%' } },
        { header: '表头', field: 'tableHeader', style: { 'text-align': 'center', 'overflow': 'hidden', 'padding': 0 } }
    ]
    /**  */
    summaryTypes: any[] = [
        { label: '总数', value: 'count' }, { label: '求和', value: 'sum' }, { label: '最大值', value: 'max' }, { label: '最小值', value: 'min' }, { label: '平均数', value: 'avg' }
    ]
    groups: any[] = [];
    summaries: any[] = [];

    constructor(
        private reportConfigService: ReportConfigService,
        private dialogService: DialogService,
        private mdlDialogService: MdlDialogService,
        private mdlSnackbarService: MdlSnackbarService) { }

    ngOnInit() {
    }

    /**添加分组条件 */
    addGroupField() {
        let ids = [];
        this.groups.forEach(e => ids.push(e['fieldId']));
        const ref = this.dialogService.open(ReportConfigFieldAddComponent, {
            'header': '添加分组条件',
            'width': '800px',
            contentStyle: { "height": "650px" },
            data: { reportId: this.currentReportId, checkedIds: ids }
        });
        ref.onClose.subscribe((data: any) => {
            if (data) {
                this.reportConfigService.saveUpdateReportStatisGroupFields(data, this.currentReportId).subscribe(data => {
                    this.reportConfigService.loadReportStatisGroupFields(this.currentReportId).subscribe(data => this.groups = data);
                });
            }
        });
    }

    /** 删除分组条件 */
    deleteGroupField(rowData) {
        this.mdlDialogService.confirm('您确定要删除这条分组信息吗？', "取消", "确定").subscribe(() => {
            this.reportConfigService.deleteReportStatisGroupField(rowData.groupFieldId).subscribe(data => {
                this.reportConfigService.loadReportStatisGroupFields(this.currentReportId).subscribe(data => this.groups = data);
            });
        });
    }

    /** 保存分组相关条件 */
    saveUpdateGroupField() {
        let temp = [];
        this.groups.forEach(e => {
            e.sumFlag ? e.sumFlag = 1 : e.sumFlag = 0;
            temp.push(e);
        });
        this.reportConfigService.saveUpdateReportStatisGroupFields(temp, this.currentReportId).subscribe(data => this.mdlSnackbarService.showToast('保存成功'));
    }


    /**添加汇总条件 */
    addSummaryField() {
        let ids = [];
        this.summaries.forEach(e => ids.push(e['fieldId']));
        const ref = this.dialogService.open(ReportConfigFieldAddComponent, {
            'header': '添加汇总条件',
            'width': '800px',
            contentStyle: { "height": "650px" },
            data: { reportId: this.currentReportId, checkedIds: ids }
        });
        ref.onClose.subscribe((data: any) => {
            if (data) {
                this.reportConfigService.saveUpdateReportStatisSummaryFields(data, this.currentReportId).subscribe(data => {
                    this.reportConfigService.loadReportStatisSummaryFields(this.currentReportId).subscribe(data => this.summaries = data);
                });
            }
        });
    }

    /** 删除汇总条件 */
    deleteSummaryField(rowData) {
        this.mdlDialogService.confirm('您确定要删除这条汇总信息吗？', "取消", "确定").subscribe(() => {
            this.reportConfigService.deleteReportStatisSummaryField(rowData.subFieldId).subscribe(data => {
                this.reportConfigService.loadReportStatisSummaryFields(this.currentReportId).subscribe(data => this.summaries = data);
            });
        });
    }

    /** 保存汇总字段 */
    saveUpdateSummaryField() {
        let temp = [];
        this.summaries.forEach(e => {
            e.detailFlag ? e.detailFlag = 1 : e.detailFlag = 0;
            temp.push(e);
        });
        this.reportConfigService.saveUpdateReportStatisSummaryFields(temp, this.currentReportId).subscribe(data => this.mdlSnackbarService.showToast('保存成功'));
    }
}
